﻿@using CubicFWNet.Data.ViewModal
@model CubicFWNet.Data.Models.OriginalCost
@{
    //ViewBag.Title = "原料成本核算";
    var productName = ViewBag.ProductName;
    var tid = ViewBag.technologyId;
}
<section class="content-header">
    <h1>
        核算查询
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("QueryOrder", "Purchase")">核算查询</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (Model != null)
                {
                    <div class="box-body form-horizontal">
                        <div class="form-group col-md-6">
                            <label for="ProductName" class="col-sm-3 control-label">产品名称</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="ProductName" name="ProductName" value="@productName" readonly="readonly">
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">工艺</label>
                            <div class="col-sm-9" style="padding-top: 7px; margin-bottom: 0;">
                                <a href="javascript:void(0)" class="technologyinfo" data-tid="@tid">查看工艺</a>
                            </div>
                        </div>
                        <input type="hidden" id="tId" name="tId" value="@tid" />
                        <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <input type="hidden" id="modalData" value="@Model.Data" readonly="readonly">
                        <div id="json-content">
                        </div>
                        <div class="form-group col-md-12" style="height: 1px;"></div>
                        <div class="form-group col-md-6">
                            <label class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo" readonly="readonly">@Model.Memo</textarea>
                            </div>
                        </div>
                        <div class="form-group col-md-12" style="height: 1px;">
                        </div>
                        <div class="form-group col-md-6">
                            <label for="operationName" class="col-sm-3 control-label">操作人</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="operationName" name="operationName" value="@Model.CreatorName" readonly="readonly">
                            </div>
                        </div>
                        <div class="form-group col-md-6">
                            <label for="operationTime" class="col-sm-3 control-label">日期</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="operationTime" name="operationTime" value="@Model.CreateTime" readonly="readonly">
                            </div>
                        </div>
                    </div>
                }

                <div class="box-footer" style="width: 80%; margin-left: 20px;">
                    <a role="button" href="@Url.Action("QueryOrder", "Purchase")" class=" btn btn-primary submitbtn">返回</a>
                </div>
            </div>
        </div>
    </div>
</section>
<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

<style>
    .has-error {
        border-color: #dd4b39;
        box-shadow: none;
    }
</style>


@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.Data = getParam();
                    //postData.Type = type;
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            $.modalAlert("操作成功", "success");
                        }
                    });
                }
            });

            var data = JSON.parse(document.getElementById("modalData").value);
            var tid = document.getElementById("tId").value;
            var $content = $('#json-content');
            var html = "";
            data.forEach(function (e, i) {
                if (typeof e.value == "undefined") {
                    e.value = "";
                }
                if (typeof e.name == "undefined") {
                    e.name = "";
                }
                if (typeof e.des == "undefined") {
                    e.des = "未知";
                }
                if (i == 0) {
                    html += "<div class='form-group col-md-6'>";
                    html += "<label class='col-sm-3 control-label'>主材料</label>";
                    html += "<div class='col-sm-9' style='padding-top: 7px; margin-bottom: 0;'>";
                    html += "<a href='javascript:void(0)' class='productinfo' data-tid='" + tid + "' data-mid='" + e.des + "'>" + e.name + "</a>";
                    html += "</div></div>";
                    html += "<div class='form-group col-md-6'>";
                    html += "<label class='col-sm-3 control-label'>成本</label>";
                    html += "<div class='col-sm-9'>";
                    html += "<input type='text' class='form-control' value='" + e.value + "' readonly='readonly'>";
                    html += "</div></div>";
                } else {
                    html += "<div class='form-group col-md-6'>";
                    html += "<label class='col-sm-3 control-label'>辅材料</label>";
                    html += "<div class='col-sm-9' style='padding-top: 7px; margin-bottom: 0;'>";
                    html += "<a href='javascript:void(0)' class='productinfo' data-tid='" + tid + "' data-mid='" + e.des + "'>" + e.name + "</a>";
                    html += "</div></div>";
                    html += "<div class='form-group col-md-6'>";
                    html += "<label class='col-sm-3 control-label'>成本</label>";
                    html += "<div class='col-sm-9'>";
                    html += "<input type='text' class='form-control' value='" + e.value + "' readonly='readonly'>";
                    html += "</div></div>";
                    //html += "<div class='form-group col-md-6'>";
                    //html += "<label class='col-sm-3 control-label'>辅原材料</label>";
                    //html += "<div class='col-sm-9' style='padding-top: 7px; margin-bottom: 0;'>";
                    //html += "<textarea class='form-control' rows='3' data-des='0' data-name='辅原材料' readonly='readonly'>" + e.name + "</textarea>";
                    //html += "</div></div>";
                    //html += "<div class='form-group col-md-6'>";
                    //html += "<label class='col-sm-3 control-label'>成本</label>";
                    //html += "<div class='col-sm-9'>";
                    //html += "<input type='text' class='form-control' value='" + e.value + "' readonly='readonly'>";
                    //html += "</div></div>";
                }

            });
            $content.append(html);
        });
        $(document).on("click", ".technologyinfo", function () {
        //$(".technologyinfo").click(function () {
            var tid = $(this).data('tid');
            //var mid = $(this).data('mid');
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "../Technology/TechnologyInfo?tId=" + tid,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        });
        $(document).on("click", ".productinfo", function () {
            var mid = $(this).data('mid');
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "../Product/Info?PID=" + mid,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        });

        function formsubmit(t) {
            var memo = $("#Memo").val();
            if (memo == "") {
                $.modalAlert("请填写备注", "warning");
                return false;
            }
            if (getParam()) {
                $("#frmedit").submit();
            }
        }

        function getParam() {
            var num = 0;
            $("#json-content").find('.materialPrice').each(function (r) {
                var $this = $(this);
                var v = $this.val();
                if (isNaN(v) || v == "") {
                    num++;
                }
            });
            if (num > 0) {
                $.modalAlert("原材料成本价格格式错误", "warning");
                return false;
            }
            var postdata = [];
            //主键
            $("#json-content").find('.materialPrice').each(function (r) {
                var $this = $(this);
                var _des = $this.attr('data-des');
                var _name = $this.attr('data-name');
                var type = $this.attr('type');
                switch (type) {
                    case "checkbox":
                        postdata.push({ des: _des, name: _name, value: $this.is(":checked") });
                        break;
                    case "select":
                        var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                        postdata.push({ des: _des, name: _name, value: value });
                        break;
                    default:
                        var value = $this.val() == "" ? "&nbsp;" : $this.val();
                        postdata.push({ des: _des, name: _name, value: value });
                        break;
                }

            });
            return JSON.stringify(postdata);
        }

        $(".okeydown").on("keyup", function () {
            var value = $(this).val();
            if (isNaN(value) || value == "" || value == "Infinity" || value == "NaN") {
                $(this).addClass("has-error");
            } else {
                $(this).removeClass("has-error");
            }
        });


        $(".okeydown").on("keydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });
    </script>
}
